<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/** @var $element \Magento\Framework\Data\Form\Element\Fieldset */
$element = $block->getElement();
$containerId = $element->getFieldsetContainerId();
$id = $element->getHtmlId();
$isCollapsable = $element->getCollapsable();
$isWrapped = $containerId || $element->getHeaderBar() || $isCollapsable;
$titleActions = '<div class="actions">' . $element->getHeaderBar() . '</div>';
$isField = $element->getFieldsetType() == 'field';
$advancedAfter = $element->getAdvancedPosition() == 'after'; // To place advanced options inside or after fieldset
$advancedLabel = $element->getAdvancedLabel();
if (!isset($advancedLabel)) {
    $advancedLabel = __('Additional Settings');
}

$cssClass = ($isField) ? 'field ' . $element->getClass() : 'fieldset admin__fieldset ' . $element->getClass();

if ($isField) {
    $count = $element->getCountBasicChildren();
    $cssClass .= ($element->hasAdvanced()) ? ' complex' : '';
}
?>

<?php
/**
* @todo investigate situations, when the following is needed:
* echo $element->getHeaderBar();
* echo $element->getSubFieldsetHtml();
*/ ?>

<?php if ($isWrapped): ?>
    <div class="fieldset-wrapper <?php echo($isCollapsable) ? 'admin__collapsible-block-wrapper ' : ''; ?>"
        id="<?php /* @escapeNotVerified */ echo $containerId ? $containerId : $id . '-wrapper';?>"
        data-role="<?php /* @escapeNotVerified */ echo $id ?>-wrapper">
        <div class="fieldset-wrapper-title admin__fieldset-wrapper-title">
            <strong <?php /* @escapeNotVerified */ echo($isCollapsable) ?
                'class="admin__collapsible-title" data-toggle="collapse" data-target="#' . $id . '-content"' :
                'class="title"'; ?>>
                <span><?php echo $block->escapeHtml($element->getLegend()) ?></span>
            </strong>
            <?php /* @escapeNotVerified */ echo $titleActions; ?>
        </div>
        <div class="fieldset-wrapper-content admin__fieldset-wrapper-content<?php echo($isCollapsable) ? ' collapse' : ''; ?>"
            id="<?php /* @escapeNotVerified */ echo $id ?>-content"
            data-role="<?php /* @escapeNotVerified */ echo $id ?>-content">
<?php endif; ?>

    <?php if (!$element->getNoContainer()): ?>
        <fieldset class="<?php /* @escapeNotVerified */ echo $cssClass ?>" id="<?php /* @escapeNotVerified */ echo $id ?>">
        <?php if ($element->getLegend() && !$isWrapped): ?>
            <legend class="<?php /* @escapeNotVerified */ echo $isField ? 'label admin__field-label' : 'admin__legend legend'?>">
                <span><?php echo $block->escapeHtml($element->getLegend()) ?></span>
            </legend><br />
        <?php endif; ?>
    <?php endif; ?>


    <div class="messages">
        <?php if ($element->getComment() && !$isField): ?>
            <div class="message message-notice"><?php echo $block->escapeHtml($element->getComment()) ?></div>
        <?php endif; ?>
    </div>


    <?php echo($isField) ? '<div class="control admin__field-control">' : ''; ?>

    <?php if ($element->hasHtmlContent() && !$isField): ?>
        <?php echo $element->getHtmlContent(); ?>
    <?php else: ?>

        <?php if ($isField && $count > 1):?>
            <div class="fields-group-<?php /* @escapeNotVerified */ echo $count?>">
        <?php endif; ?>

        <?php echo $element->getBasicChildrenHtml(); ?>

        <?php echo($isField && $count > 1) ? '</div>' : '' ?>

        <?php if ($element->getComment() && $isField): ?>
            <div class="note"><?php echo $block->escapeHtml($element->getComment()) ?></div>
        <?php endif; ?>

        <?php if ($element->hasAdvanced() && !$isField): ?>
            <?php echo(!$element->getNoContainer() && $advancedAfter)  ? '</fieldset>' : ''?>
            <details data-mage-init='{"details": {}}' class="details admin__collapsible-block-wrapper" id="details<?php /* @escapeNotVerified */ echo $id ?>">
                <summary class="details-summary admin__collapsible-title" id="details-summary<?php /* @escapeNotVerified */ echo $id ?>">
                    <span><?php /* @escapeNotVerified */ echo $advancedLabel ?></span>
                </summary>
                <div class="details-content admin__fieldset" id="details-content<?php /* @escapeNotVerified */ echo $id ?>">
                    <?php echo $element->getAdvancedChildrenHtml(); ?>
                </div>
            </details>
        <?php elseif ($element->hasAdvanced() && $isField): ?>
            <div class="nested" id="nested<?php /* @escapeNotVerified */ echo $id ?>">
                <?php echo $element->getAdvancedChildrenHtml(); ?>
            </div>
        <?php endif; ?>

        <?php echo($isField) ? '</div>' : ''; ?>

    <?php endif; ?>


    <?php if (!$element->getNoContainer() && !$advancedAfter): ?>
        </fieldset>
    <?php endif; ?>

<?php if ($isWrapped): ?>
        </div>
    </div>
<?php endif; ?>
